package com.cn.zpark.servlet;

import com.cn.zpark.beans.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.cn.zpark.servlet
 * @ClassName: ServletDem
 * @Description:
 * @author: RZS
 * @date: 2022/4/20  17:31
 */
@WebServlet("/sd4.do")
public class ServletDemo04 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 绑定数组、集合、对象到session域

        // 获取session对象
        HttpSession session = request.getSession();
        // 创建数组
        int[] arr = {112, 119, 110};
        // 绑定数据
        session.setAttribute("array", arr);

        // 绑定list集合数据
        List<User> list = new ArrayList<>();
        list.add(new User(1, "张三1", "123"));
        list.add(new User(2, "张三2", "456"));
        list.add(new User(3, "张三3", "789"));

        session.setAttribute("lists", list);

        // 绑定MAP集合数据
        Map<String, String> map = new HashMap<>();
        map.put("k1", "v1");
        map.put("k2", "v2");
        map.put("k3", "v3");

        session.setAttribute("map", map);

        // 绑定javaBean
        User user = new User();
        user.setId(4);
        user.setUsername("李四");
        user.setPassword("123456");

        session.setAttribute("user", user);

        // 转发给demo04.jsp处理
        request.getRequestDispatcher("jsp/demo04.jsp").forward(request, response);
    }
}
